﻿//-----------------------------------------------------------------------
// <copyright file="Tagger.cs" company="iTunesEncodeNet">
//  (c) 2009 Vlasenko Dmitry
// </copyright>
//-----------------------------------------------------------------------

namespace ITunesEncodeNet
{
    /// <summary>
    /// Provides tagging methods.
    /// </summary>
    internal class Tagger
    {
        /// <summary>
        /// Encoder type.
        /// </summary>
        private EncoderOptions.EncoderType type;

        ////private Dictionary<string, Dictionary<string, string>> tagsContainer;

        /// <summary>
        /// Initializes a new instance of the Tagger class.
        /// </summary>
        /// <param name="type">Encoder type.</param>
        public Tagger(EncoderOptions.EncoderType type)
        {
            ////this.tagsContainer = new Dictionary<string, Dictionary<string, string>>();
            this.type = type;
        }

        ////private bool GetTags(string fileName)
        ////{
        ////    switch (this.type)
        ////    {
        ////        case (EncoderOptions.EncoderType.flac):
        ////            ProcessStartInfo metaflac = new ProcessStartInfo(EncoderOptions.MetaflacPath, string.Format("--list --block-type=VORBIS_COMMENT \"{0}\"", fileName));

        ////            metaflac.CreateNoWindow = true;
        ////            metaflac.UseShellExecute = false;
        ////            metaflac.RedirectStandardOutput = true;

        ////            Process process = Process.Start(metaflac);
        ////            StreamReader reader = process.StandardOutput;

        ////            process.WaitForExit();

        ////            if (process.ExitCode != 0)
        ////            {
        ////                return false;
        ////            }

        ////            string result = reader.ReadToEnd();
        ////            string[] lines = result.Split('\n');

        ////            Dictionary<string, string> tags = new Dictionary<string, string>();

        ////            foreach (string line in lines)
        ////            {
        ////                if (line.Contains("comment["))
        ////                {
        ////                    int startIndex = line.IndexOf("]:") + 2;

        ////                    string temp = line.Substring(startIndex).Trim();
        ////                    string[] tag = temp.Split('=');

        ////                    tags.Add(tag[0].Trim(), tag[1].Trim());
        ////                }
        ////            }

        ////            if (tags.Count == 0)
        ////            {
        ////                return false;
        ////            }

        ////            this.tagsContainer.Add(fileName, tags);

        ////            break;
        ////    }

        ////    return true;
        ////}

        ////private string GetTag(string fileName, string tagName)
        ////{
        ////    if (!this.tagsContainer.ContainsKey(fileName))
        ////    {
        ////        if (this.GetTags(fileName) != true)
        ////        {
        ////            return null;
        ////        }
        ////    }

        ////    Dictionary<string, string> tags = this.tagsContainer[fileName];

        ////    if (!tags.ContainsKey(tagName))
        ////    {
        ////        return null;
        ////    }

        ////    return tags[tagName];
        ////}

        /// <summary>
        /// Gets track artist.
        /// </summary>
        /// <param name="fileName">Full path to file.</param>
        /// <returns>Track artist.</returns>
        public string GetArtist(string fileName)
        {
            ////return this.GetTag(fileName, "ARTIST");

            TagLib.File file = TagLib.File.Create(fileName);
            return file.Tag.FirstArtist;
        }

        /// <summary>
        /// Gets track name.
        /// </summary>
        /// <param name="fileName">Full path to file.</param>
        /// <returns>Track name.</returns>
        public string GetName(string fileName)
        {
            ////return this.GetTag(fileName, "TITLE");

            TagLib.File file = TagLib.File.Create(fileName);
            return file.Tag.Title;
        }

        /// <summary>
        /// Gets album title.
        /// </summary>
        /// <param name="fileName">Full path to file.</param>
        /// <returns>Album title.</returns>
        public string GetAlbum(string fileName)
        {
            ////return this.GetTag(fileName, "ALBUM");

            TagLib.File file = TagLib.File.Create(fileName);
            return file.Tag.Album;
        }

        /// <summary>
        /// Gets track number.
        /// </summary>
        /// <param name="fileName">Full path to file.</param>
        /// <returns>Track number.</returns>
        public uint? GetTrackNumber(string fileName)
        {
            ////string tag = this.GetTag(fileName, "TRACKNUMBER");

            ////if (string.IsNullOrEmpty(tag))
            ////{
            ////    return null;
            ////}

            ////return int.Parse(tag);

            TagLib.File file = TagLib.File.Create(fileName);
            return file.Tag.Track;
        }

        /// <summary>
        /// Gets publication year.
        /// </summary>
        /// <param name="fileName">Full path to file.</param>
        /// <returns>Publication year.</returns>
        public uint? GetYear(string fileName)
        {
            ////string tag = this.GetTag(fileName, "DATE");

            ////if (string.IsNullOrEmpty(tag))
            ////{
            ////    return null;
            ////}

            ////return int.Parse(tag);

            TagLib.File file = TagLib.File.Create(fileName);
            return file.Tag.Year;
        }

        /// <summary>
        /// Gets track genre.
        /// </summary>
        /// <param name="fileName">Full path to file.</param>
        /// <returns>Track genre.</returns>
        public string GetGenre(string fileName)
        {
            ////return this.GetTag(fileName, "GENRE");

            TagLib.File file = TagLib.File.Create(fileName);
            return file.Tag.FirstGenre;
        }

        /// <summary>
        /// Gets track comment.
        /// </summary>
        /// <param name="fileName">Full path to file.</param>
        /// <returns>Track comment.</returns>
        public string GetComment(string fileName)
        {
            ////return this.GetTag(fileName, "COMMENT");

            TagLib.File file = TagLib.File.Create(fileName);
            return file.Tag.Comment;
        }
    }
}
